package cn.tedu.tmall.admin.content.controller;

import cn.tedu.tmall.admin.content.dao.mapper.CategoryMapper;
import cn.tedu.tmall.admin.content.pojo.param.CategoryAddParam;
import cn.tedu.tmall.admin.content.serivce.ICategoryService;
import cn.tedu.tmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 管理端分类管理相关接口
 */
@Slf4j
@Api(tags = "类别管理相关接口")
@RestController
@RequestMapping("/category")
public class CategoryController {

    //@Autowired
    //@Resource(name = "ACategoryService")
    //@Autowired 和 @Resource都能加载,都加载的是CategoryService
    @Resource
    private ICategoryService categoryService;

    /**
     * 语法上是没有问题的,但是不推荐
     */
    //@Autowired
    //private CategoryMapper categoryMapper;

    /**
     * 增加分类
     * @param categoryAddParam 分类详情
     */
    @ApiOperation("增加类别")
    @PreAuthorize("hasAuthority('/article/add')")
    public JsonResult addCategory(@Validated @RequestBody CategoryAddParam categoryAddParam) {
        //快速失败,
        //1 验证入参
        // 1 分类名称不能为空
        // 2 分类名称的长度不能超过4个
        // 上面的验证逻辑应该写在哪?
        //   1 controller
        //   2
        log.debug("增加分类接口入参:{}",categoryAddParam);
        categoryService.addCategory(categoryAddParam);
        //JsonResult jsonResult = new JsonResult();
        //jsonResult.setCode(1000);
        //jsonResult.setMsg("ok");
        //return jsonResult;
        JsonResult ok = JsonResult.ok();
        log.debug("增加分类接口入参:{},出参:{}",categoryAddParam,ok);
        return ok;
    }
}
